package org.zhouhe.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.zhouhe.web.tag.function.PanelResetFunction;
import org.zhouhe.web.tag.function.PanelSubmitFunction;

public class QueryPanel extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String url;
	private String submitFunctionName;
	private String resetFunctionName;
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			SimplePage pageTag = (SimplePage) this.getParent();
			submitFunctionName = pageTag.getId() +"_submit_query";
			resetFunctionName = pageTag.getId() +"_reset_query";
			String width = pageTag.getWidth();
			if(width != null){
				width = "width:"+width+(width.indexOf("%")>-1 ? "":"px") +";";
			}else{
				width = "";
			}
			out.println("<div data-options=\"region:'north'\" title='查询条件'>");
			out.println("	<form action='#' onsubmit='return false;'>");
			out.println("		<div class='queryPanel'>");
			pageTag.addFunction(new PanelSubmitFunction(submitFunctionName, url).toString());
			pageTag.addFunction(new PanelResetFunction(resetFunctionName).toString());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.println("			<div class='dialog-button' style='text-align:center;'>");
			out.println("				<a href='#' onclick='"+ submitFunctionName +"(this)' class='easyui-linkbutton' data-options=\"iconCls:'icon-search'\">查询</a>");
			out.println("				<a href='#' onclick='"+ resetFunctionName +"(this)' class='easyui-linkbutton' data-options=\"iconCls:'icon-reload'\">重置</a>");
			out.println("			</div>");
			out.println("		</div>");
			out.println("	</form>");
			out.println("</div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
